{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3d4f52b-e58d-43b9-bd3e-9a1c23d5ec7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "suppressMessages(library(ArchR))\n",
    "suppressMessages(library(patchwork))\n",
    "suppressMessages(library(ggplot2))\n",
    "suppressMessages(library(dplyr))\n",
    "suppressMessages(library(cowplot))\n",
    "suppressMessages(library(ggrepel))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "375fa5f7-e4b0-4109-a74b-2541bb7214a8",
   "metadata": {},
   "source": [
    "### Set parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f98caa1e-6cdb-4417-bbbe-69873992090a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "set.seed(42)\n",
    "addArchRThreads(threads = parallel::detectCores() - 2)\n",
    "addArchRGenome(\"hg38\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da663ec1-1b79-40aa-bc1e-a7a32390511a",
   "metadata": {},
   "outputs": [],
   "source": [
    "proj <- loadArchRProject(\"../data/VisiumHeart\", showLogo = FALSE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d065a7e8-1d9a-4cf7-be09-d9e532712aa5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# number of cells per sample\n",
    "df <- proj@cellColData %>%\n",
    "    as.data.frame() %>%\n",
    "    group_by(Sample) %>%\n",
    "    summarise(n_cells = n(),\n",
    "             n_fragments = sum(nFrags))\n",
    "\n",
    "p <- ggplot(data = df, aes(x = reorder(Sample, -n_cells), y = log10(n_cells))) +\n",
    "    geom_bar(stat = \"identity\") +\n",
    "    geom_text(aes(label=n_cells), position=position_dodge(width=0.9), vjust=-0.25) +\n",
    "    xlab(\"\") + ylab(\"Number of cells (log10)\") +\n",
    "    theme_cowplot() +\n",
    "    theme(axis.text.x = element_text(angle = 60, hjust = 1))\n",
    "\n",
    "options(repr.plot.width = 20, repr.plot.height = 5)\n",
    "\n",
    "p"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9835604-54f8-4215-9451-5d3a930948e3",
   "metadata": {},
   "source": [
    "### Visualize quality control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83ce0b82-5131-4947-8491-06119dd2ffe4",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Visualize data quality\n",
    "p1 <- plotGroups(ArchRProj = proj, \n",
    "                 groupBy = \"Sample\", \n",
    "                 colorBy = \"cellColData\", \n",
    "                 name = \"TSSEnrichment\",\n",
    "                 alpha = 0.4,\n",
    "                 plotAs = \"violin\",\n",
    "                 addBoxPlot = TRUE)\n",
    "\n",
    "p2 <- plotGroups(ArchRProj = proj, \n",
    "                 groupBy = \"Sample\", \n",
    "                 colorBy = \"cellColData\", \n",
    "                 name = \"log10(nFrags)\",\n",
    "                 plotAs = \"violin\",\n",
    "                 alpha = 0.4,\n",
    "                 addBoxPlot = TRUE)\n",
    "\n",
    "options(repr.plot.width = 10, repr.plot.height = 5)\n",
    "\n",
    "p1\n",
    "p2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c1c8767-a1db-49ad-bf61-326e783b5838",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Session information\n",
    "\n",
    "sessionInfo()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.0.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
